figure();hold on
refrence=patch([0 1 0],[1 0 0],'r');
car=patch([0 1 0],[1 0 0],'b');
plot(x(:,1),x(:,2))
plot(x(:,4),x(:,5))

    axis equal

for it=1:100:length(t)
    title(sprintf("t=%.2fs",t(it)))
    update_car(car,x(it,1),x(it,2),x(it,3))
    update_car(refrence,x(it,4),x(it,5),x(it,6))
    axis([-1 3 0 3.3])
    drawnow limitrate
   exportgraphics(gca,"car.gif","Append",it>1);
    pause(0.1)
    
end

function update_car(h,x,y,theta)
    height=0.2;
    width=0.05;
    h.XData=[x+height*cos(theta) x+width*cos(theta+pi/2) x+width*cos(theta-pi/2)];
    h.YData=[y+height*sin(theta) y+width*sin(theta+pi/2) y+width*sin(theta-pi/2)];
end